/*
    This file is part of Lynsync

    Lynsync is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Lynsync is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Lynsync; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Lynsync
{
    public partial class WorldOptionsPage : Form
    {
    	MainWindow MyMainWindow;

        public WorldOptionsPage(MainWindow mainWindow)
        {
        	MyMainWindow = mainWindow;
        	
            InitializeComponent();
            SetOptionNames();
			//System.Windows.Forms.Control mainWindow = this.Parent;
            
			textBoxLynsyncKey.Text = mainWindow.GetWorldKey();
			
            //string Text = MyMainWindow.GetWorldName();
            //textBoxWorld.Text = Text;
            //textBoxWorld.Select(0, 1);
            
            updateFromKey();
            UpdateFormBasedOnChange();
        }
        //---------------------------------------------------------------------------------------------
        void SetOptionNames()
        {
            this.CheckBoxLocalVault.Name = "local";
            this.CheckBoxCopyBuf.Name = "copyBuf";
            this.CheckBoxPopUpLogin.Name = "popup";
            this.CheckBoxStartNWN.Name = "startApp";
            this.CheckBoxPassLoginInfo.Name = "passLogin";
            this.CheckBoxMyDocuments.Name = "useMyDocs";
            this.CheckBoxConfirmFolder.Name = "confirmFolder";
            this.CheckBoxConfirmExt.Name = "confirmExt";
            this.CheckBoxCharCreate.Name = "charCreate";
            this.CheckBoxExtra.Name = "extra";
            this.CheckBoxHak.Name = "iso_hak";
            this.CheckBoxOverride.Name = "iso_override";
            this.CheckBoxPortraits.Name = "iso_portraits";
            this.CheckBoxTlk.Name = "iso_tlk";        	
            this.CheckBoxUiCustom.Name = "iso_uiCustom";        	
            this.CheckBoxUiDefault.Name = "iso_uiDefault";        	
        }
        //---------------------------------------------------------------------------------------------
        void UpdateFormBasedOnChange()
        {
            if (rdoNWN.Checked)
        	{
        		labelMyDocuments.Enabled = false;
        		CheckBoxMyDocuments.Checked = false;
        		CheckBoxMyDocuments.Enabled = false;
        	}
        	else
        	{
        		labelMyDocuments.Enabled = true;
        		CheckBoxMyDocuments.Enabled = true;
        	}

        	comboBoxNWNApp.Enabled = CheckBoxStartNWN.Checked;
        	CheckBoxPassLoginInfo.Enabled = CheckBoxStartNWN.Checked;
        	labelPassLoginInfo.Enabled = CheckBoxStartNWN.Checked;
        	if (comboBoxNWNApp.Enabled)
        	{
        		if (rdoNWN.Checked)
        		{
        			comboBoxNWNApp.Text = "nwnmain.exe";
        			comboBoxNWNApp.Items.Clear();
        			comboBoxNWNApp.Items.AddRange(new object[] {"nwnmain.exe"});
        		}
        		else
        		{
        			comboBoxNWNApp.Text = "nwn2main.exe";
        			comboBoxNWNApp.Items.Clear();
        			comboBoxNWNApp.Items.AddRange(new object[] {"nwn2main.exe", "nwn2amcmain.exe"});
        		}
        	}
        	else
        	{
        		comboBoxNWNApp.Text = "";
        	}
       		addElementToKey(7, comboBoxNWNApp.Text);


        }
        //---------------------------------------------------------------------------------------------
        public void updateFromKey()
        {
        	string key = textBoxLynsyncKey.Text;
        	string[] splitLine = Regex.Split(key," -- ");
        	int length = splitLine.Length;
        	textBoxWorld.Text = splitLine[0];
        	if (length==1) return;
        	comboBoxType.Text = splitLine[1];
        	if (length==2) return;
        	textBoxHomePage.Text = splitLine[2];
        	buttonBrowse.Enabled = (splitLine[2] != "");
          	if (length==3) return;
        	textBoxRemote.Text = splitLine[3];
        	buttonRemoteCheck.Enabled = (splitLine[3] != "");
          	if (length==4) return;
        	textBoxWorldFolder.Text = splitLine[4];
        	buttonUninstall.Enabled = (splitLine[4] != "");
          	if (length==5) return;
        	textBoxServer.Text = splitLine[5];
          	if (length==6) return;
        	textBoxServerPassword.Text = splitLine[6];
          	if (length==7) return;
          	CheckBoxStartNWN.Checked = (splitLine[7] != ""); 
        	comboBoxNWNApp.Text = splitLine[7];
          	if (length==8) return;
          	updateOptions(splitLine[8]);
        }
        //---------------------------------------------------------------------------------------------
        public void updateOptions(string line)
        {
        	string[] options = line.Split();
        	bool cb_nwn2 = false;
        	bool cb_local = false;
        	bool cb_copyBuff = false;
        	bool cb_popup = false;
//        	bool cb_startApp = false;
        	bool cb_passLogin = false;
        	bool cb_useMyDocs = false;
        	bool cb_confirmFolder = false;
        	bool cb_confirmExt = false;
        	bool cb_charCreate = false;
        	bool cb_extra = false;
        	bool cb_iso_hak = false;
        	bool cb_iso_override = false;
        	bool cb_iso_portraits = false;
        	bool cb_iso_tlk = false;
        	bool cb_iso_uiCustom = false;
        	bool cb_iso_uiDefault = false;
        	for(int index=0; index<options.Length; index++)
        	{
//        		MyMainWindow.AddTextToStatusBox("option= "+options[index]+"\r\n");
        		if (options[index] == "nwn2") cb_nwn2 = true;
        		else if (options[index] == CheckBoxLocalVault.Name) cb_local = true; 
        		else if (options[index] == CheckBoxCopyBuf.Name) cb_copyBuff = true; 
        		else if (options[index] == CheckBoxPopUpLogin.Name) cb_popup = true; 
//        		else if (options[index] == CheckBoxStartNWN.Name) cb_startApp = true; 
        		else if (options[index] == CheckBoxPassLoginInfo.Name) cb_passLogin = true; 
        		else if (options[index] == CheckBoxMyDocuments.Name) cb_useMyDocs = true; 
        		else if (options[index] == CheckBoxConfirmFolder.Name) cb_confirmFolder = true; 
        		else if (options[index] == CheckBoxConfirmExt.Name) cb_confirmExt = true; 
        		else if (options[index] == CheckBoxCharCreate.Name) cb_charCreate = true; 
        		else if (options[index] == CheckBoxExtra.Name) cb_extra = true; 
        		else if (options[index] == CheckBoxHak.Name) cb_iso_hak = true; 
        		else if (options[index] == CheckBoxOverride.Name) cb_iso_override = true; 
        		else if (options[index] == CheckBoxPortraits.Name) cb_iso_portraits = true; 
        		else if (options[index] == CheckBoxTlk.Name) cb_iso_tlk = true; 
        		else if (options[index] == CheckBoxUiCustom.Name) cb_iso_uiCustom = true; 
        		else if (options[index] == CheckBoxUiDefault.Name) cb_iso_uiDefault = true; 
        		
        	}
        	rdoNWN.Checked = !cb_nwn2;
        	rdoNWN2.Checked = cb_nwn2;
        	CheckBoxLocalVault.Checked = cb_local;
        	CheckBoxCopyBuf.Checked = cb_copyBuff;
        	CheckBoxPopUpLogin.Checked = cb_popup;
//        	CheckBoxStartNWN.Checked = cb_startApp;
			CheckBoxPassLoginInfo.Checked = cb_passLogin;
        	CheckBoxMyDocuments.Checked = cb_useMyDocs;
        	CheckBoxConfirmFolder.Checked = cb_confirmFolder;
        	CheckBoxConfirmExt.Checked = cb_confirmExt;
        	CheckBoxCharCreate.Checked = cb_charCreate;
        	CheckBoxExtra.Checked = cb_extra;
        	CheckBoxHak.Checked = cb_iso_hak;
        	CheckBoxOverride.Checked = cb_iso_override;
        	CheckBoxPortraits.Checked = cb_iso_portraits;
        	CheckBoxTlk.Checked = cb_iso_tlk;
        	CheckBoxUiCustom.Checked = cb_iso_uiCustom;
        	CheckBoxUiDefault.Checked = cb_iso_uiDefault;
        	        	
        }
        
        //---------------------------------------------------------------------------------------------
        public void textBoxLynsyncKeyChanged(object sender, EventArgs e)
        {
        	updateFromKey();
        }

        //---------------------------------------------------------------------------------------------
        public void addElementToKey(int index, string newElement)
        {
        	string key = textBoxLynsyncKey.Text;
        	string[] splitLine = Regex.Split(key," -- ");
        	int totalLength = splitLine.Length;
        	if (totalLength <= index)
        	{
        		for(;totalLength<=index;totalLength++) key += " --  ";
        		splitLine = Regex.Split(key," -- ");
//        	    MyMainWindow.AddTextToStatusBox("key= "+key+"\r\n");
        	}
        	if (splitLine[index] != newElement)
        	{
        		splitLine[index] = newElement;
        		textBoxLynsyncKey.Text = string.Join(" -- ", splitLine);
        	}
        }
        //---------------------------------------------------------------------------------------------
        private string joinMyOptions(string options, string newOption)
        {
        	if (options != "") options += " ";
        	return options + newOption;
        }
        //---------------------------------------------------------------------------------------------
        public void addOption(string option, bool isChecked)
        {
        	string key = textBoxLynsyncKey.Text;
        	string[] splitLine = Regex.Split(key," -- ");
        	int totalLength = splitLine.Length;
        	int index = 8;
        	if (totalLength <= index)
        	{
        		for(;totalLength<=index;totalLength++) key += " --  ";
        		splitLine = Regex.Split(key," -- ");
//        	    MyMainWindow.AddTextToStatusBox("key= "+key+"\r\n");
        	}
        	string line = splitLine[index];
        	string[] options = line.Split();
        	string newElement = "";
        	bool wasFound = false;
        	for(int j=0; j<options.Length; j++)
        	{
        		if (options[j] == option) 
        		{
        			wasFound = true;
        		    if (isChecked) newElement = joinMyOptions(newElement, option);
        		}
        		else 
        		{
        			newElement = joinMyOptions(newElement, options[j]);
        		}
        	}
        	if (isChecked && !wasFound) newElement = joinMyOptions(newElement, option);
        	
         	if (line != newElement)
        	{
        		splitLine[index] = newElement;
        		textBoxLynsyncKey.Text = string.Join(" -- ", splitLine);
        	}
       
        }
        //---------------------------------------------------------------------------------------------
        public void textBoxWorldChanged(object sender, EventArgs e)
        {
        	addElementToKey(0, textBoxWorld.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void comboBoxTypeChanged(object sender, EventArgs e)
        {
        	addElementToKey(1, comboBoxType.Text);
        }       
        //---------------------------------------------------------------------------------------------
        public void textBoxHomePageChanged(object sender, EventArgs e)
        {
        	addElementToKey(2, textBoxHomePage.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void textBoxRemoteChanged(object sender, EventArgs e)
        {
        	addElementToKey(3, textBoxRemote.Text);
        	buttonRemoteCheck.ForeColor = ForeColor;
        }
        //---------------------------------------------------------------------------------------------
        public void textBoxWorldFolderChanged(object sender, EventArgs e)
        {
        	addElementToKey(4, textBoxWorldFolder.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void textBoxServerChanged(object sender, EventArgs e)
        {
        	addElementToKey(5, textBoxServer.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void textBoxServerPasswordChanged(object sender, EventArgs e)
        {
        	addElementToKey(6, textBoxServerPassword.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void comboBoxNWNAppChanged(object sender, EventArgs e)
        {
        	addElementToKey(7, comboBoxNWNApp.Text);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxLocalVaultChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxLocalVault.Name, CheckBoxLocalVault.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxCopyBufChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxCopyBuf.Name, CheckBoxCopyBuf.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxPopUpLoginChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxPopUpLogin.Name, CheckBoxPopUpLogin.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxStartNWNChanged(object sender, EventArgs e)
        {
        	// addOption(CheckBoxStartNWN.Name, CheckBoxStartNWN.Checked);
        	UpdateFormBasedOnChange();        	
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxPassLoginInfoChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxPassLoginInfo.Name, CheckBoxPassLoginInfo.Checked);
        }      
        //---------------------------------------------------------------------------------------------
        public void CheckBoxMyDocumentsChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxMyDocuments.Name, CheckBoxMyDocuments.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxConfirmFolderChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxConfirmFolder.Name, CheckBoxConfirmFolder.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxConfirmExtChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxConfirmExt.Name, CheckBoxConfirmExt.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxCharCreateChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxCharCreate.Name, CheckBoxCharCreate.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxExtraChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxExtra.Name, CheckBoxExtra.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxHakChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxHak.Name, CheckBoxHak.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxOverrideChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxOverride.Name, CheckBoxOverride.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxPortraitsChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxPortraits.Name, CheckBoxPortraits.Checked);
        }       
        //---------------------------------------------------------------------------------------------
        public void CheckBoxTlkChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxTlk.Name, CheckBoxTlk.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxUiCustomChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxUiCustom.Name, CheckBoxUiCustom.Checked);
        }
        //---------------------------------------------------------------------------------------------
        public void CheckBoxUiDefaultChanged(object sender, EventArgs e)
        {
        	addOption(CheckBoxUiDefault.Name, CheckBoxUiDefault.Checked);
        }
        
        //---------------------------------------------------------------------------------------------
        public void rdoNWN_CheckedChanged(object sender, EventArgs e)
        {
       	    addOption("nwn2", rdoNWN2.Checked);
			UpdateFormBasedOnChange();
        }
        //---------------------------------------------------------------------------------------------
        public void rdoNWN2_CheckedChanged(object sender, EventArgs e)
        {
       	    addOption("nwn2", rdoNWN2.Checked);
			UpdateFormBasedOnChange();
        }
       
        //---------------------------------------------------------------------------------------------
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
        	string website = "";
            try
            {
        	    website = "http://" + textBoxHomePage.Text;
        	    MyMainWindow.AddTextToStatusBox("Browse "+website+"\r\n");
                System.Diagnostics.Process.Start(website);
            }
            catch
            {
                MessageBox.Show("Sorry, I can't browse the site:" + website + "\n","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        //---------------------------------------------------------------------------------------------
        private void buttonRemoteCheck_Click(object sender, EventArgs e)
        {
        	string iniFile = "";
            try
            {
        	    iniFile = textBoxRemote.Text;
        	    MyMainWindow.AddTextToStatusBox("Browse "+iniFile+"\r\n");
                System.Diagnostics.Process.Start(iniFile);
            }
            catch
            {
                MessageBox.Show("Sorry, I can't browse the file:" + iniFile + "\n","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
//        	string status = MyMainWindow.TestRemoteIni(textBoxRemote.Text);
//			if (status == "ok") buttonRemoteCheck.ForeColor = System.Drawing.Color.Green;
//			else buttonRemoteCheck.ForeColor = System.Drawing.Color.Red;
        }
        
        //---------------------------------------------------------------------------------------------
        private void buttonUninstall_Click(object sender, EventArgs e)
        {
			MyMainWindow.SetPaths(rdoNWN2.Checked, CheckBoxMyDocuments.Checked);
			
			string[] isoFolders = {""};
        	MyMainWindow.UninstallWorldStorage(textBoxWorldFolder.Text, isoFolders);
        }
        //---------------------------------------------------------------------------------------------
        private void buttonTestEnter_Click(object sender, EventArgs e)
        {
			MyMainWindow.SetPaths(rdoNWN2.Checked, CheckBoxMyDocuments.Checked);
        	string status = MyMainWindow.EnterWorld(textBoxWorld.Text,
			                                        comboBoxNWNApp.Text,
        	                                        textBoxServer.Text, 
        	                                        textBoxServerPassword.Text,
        	                                        CheckBoxPopUpLogin.Checked,
        	                                        CheckBoxCopyBuf.Checked,
        	                                        CheckBoxPassLoginInfo.Checked
        	                                       );
			if (status == "ok") buttonTestEnter.ForeColor = System.Drawing.Color.Green;
			else buttonTestEnter.ForeColor = System.Drawing.Color.Red;
        }
       
        //---------------------------------------------------------------------------------------------
        private void buttonPrevious_Click(object sender, EventArgs e)
        {
			MyMainWindow.PreviousWorldKey();
			textBoxLynsyncKey.Text = MyMainWindow.GetWorldKey();
        	updateFromKey();
            UpdateFormBasedOnChange();

        }
        //---------------------------------------------------------------------------------------------
        private void buttonNext_Click(object sender, EventArgs e)
        {
			MyMainWindow.NextWorldKey();
			textBoxLynsyncKey.Text = MyMainWindow.GetWorldKey();
        	updateFromKey();
            UpdateFormBasedOnChange();
        }
        //---------------------------------------------------------------------------------------------
        private void new_button_Click(object sender, EventArgs e)
        {
        	MyMainWindow.AddNewKey(textBoxLynsyncKey.Text);
        	Close();
        }
        //---------------------------------------------------------------------------------------------
        private void ok_button_Click(object sender, EventArgs e)
        {
        	MyMainWindow.SaveKey(textBoxLynsyncKey.Text);
        	Close();
        }
        //---------------------------------------------------------------------------------------------
        private void cancel_button_Click(object sender, EventArgs e)
        {
        	Close();
        }
        
    }
}
